<script setup>
import {
    Edit,
    Delete
} from '@element-plus/icons-vue'
import { ref } from 'vue'
import { ruleshowListService, ruleAddService, ruleUpdateService, ruleDeleteService } from '@/api/rule.js'
import request from '@/utils/request.js'

// 将 rules 初始化为空数组
const rules = ref([])

//声明异步函数
const ruleList = async()=>{
    let result = await ruleshowListService();
    rules.value = result.data;
}
ruleList();

//控制添加分类弹窗
const dialogVisible = ref(false)

//添加分类数据模型
const ruleModel = ref({
    ruleId: 0,
    ruleName: '',
    ruleContent: ''
})
//添加分类表单校验
const ruleRules = {
    ruleId:[
        { required: true, message: '请输入规则编号', trigger: 'blur' },
    ],
    ruleName: [
        { required: true, message: '请输入规则名称', trigger: 'blur' },
    ],
    ruleContent: [
        { required: true, message: '请输入规则内容', trigger: 'blur' },
    ]
}

//调用接口，添加表单
import { ElMessage } from 'element-plus'
const addRule = async() =>{
    //调用接口
    let result = await ruleAddService(ruleModel.value)
    ElMessage.success(result.msg?result.msg:'添加成功')

    //调用分类函数
    ruleList();
    dialogVisible.value = false;
}

//定义变量，控制标题展示
const title = ref('')

//展示编辑弹窗
const showDialog = (row) =>{
    dialogVisible.value = true;
    title.value = '编辑分类';
    //数据拷贝
    ruleModel.value.ruleId = row.ruleId;
    ruleModel.value.ruleName = row.ruleName;
    ruleModel.value.ruleContent = row.ruleContent;

}

//编辑分类
const updateRule = async() =>{
    //调用接口
    let result = await ruleUpdateService(ruleModel.value)
    ElMessage.success(result.msg?result.msg:'修改成功')

    //调用分类函数
    ruleList();
    //隐藏弹窗
    dialogVisible.value = false;
}

//清空模型的数据
const clearData = () =>{
    // 修改为 0
    ruleModel.value.ruleId = 0; 
    ruleModel.value.ruleName = '';
    ruleModel.value.ruleContent = '';
}

import { ElMessageBox } from 'element-plus'
//删除规则
const deleteRule = async(row) =>{
    ElMessageBox.confirm(
        '你确认要删除信息吗？？？？？',
        '温馨提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
    .then(async() => {
        //调用接口
        let result =await ruleDeleteService(row.ruleId);
        ElMessage({
            type: 'success',
            message: '删除成功',
        })
        //刷新列表
        ruleList();
    })
    .catch(() => {
        ElMessage({
            type: 'info',
            message: '取消删除',
      })
    })
}

</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>奖学金规则分类</span>
                <div class="extra">
                    <el-button type="primary" @click="dialogVisible = true; title='添加规则'; clearData()">添加规则</el-button>
                </div>
            </div>
        </template>
        <el-table :data="rules" style="width: 100%">
            <el-table-column label="序号" prop="ruleId"> </el-table-column>
            <el-table-column label="奖学金" prop="ruleName"></el-table-column>
            <el-table-column label="奖学金评比要求" prop="ruleContent"></el-table-column>
            <el-table-column label="操作" width="100">
                <template #default="{ row }">
                    <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)"></el-button>
                    <el-button :icon="Delete" circle plain type="danger" @click="deleteRule(row)"></el-button>
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>
        <!-- 添加分类弹窗 -->
        <el-dialog v-model="dialogVisible" :title="title" width="30%">
            <el-form :model="ruleModel" :rules="ruleRules" label-width="100px" style="padding-right: 30px">
                 <el-form-item label="规则编号" prop="ruleId">
                    <el-input-number 
                        v-model="ruleModel.ruleId" 
                        :min="1" 
                        :precision="0" 
                        :step="1"
                        :max="9999999999"
                    ></el-input-number>
                </el-form-item>
                <el-form-item label="规则名称" prop="ruleName">
                    <el-input v-model="ruleModel.ruleName" minlength="1" maxlength="10"></el-input>
                </el-form-item>
                <el-form-item label="规则内容" prop="ruleContent">
                    <el-input v-model="ruleModel.ruleContent" minlength="1" maxlength="50"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="title == '添加规则'?addRule() : updateRule()"> 确认 </el-button>
                </span>
            </template>
        </el-dialog>

    </el-card>
</template>

<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}
</style>
